package com.example.zwh.computershop.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.User;


public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText uname;
    private EditText pswone;
    private EditText pswtwo;
    private Button regis;
    private Myapp myapp;

    private String em;
    private String pso;
    private String pst;

    private Toast toast;
    private Intent intent;

    private User user;
    private int state;

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        toolbar = (Toolbar) findViewById(R.id.toolBar);
        toolbar.setTitle("");

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ActivityCollector.addActivity(this);
        ininView();
        initEvent();
    }

    public void ininView() {
        uname = (EditText) findViewById(R.id.uname);
        pswone = (EditText) findViewById(R.id.pswone);
        pswtwo = (EditText) findViewById(R.id.pswtwo);
        regis = (Button) findViewById(R.id.register);
        myapp = (Myapp) getApplication();
        user = new User();
    }

    public void initEvent() {
        regis.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.register:
                em = uname.getText().toString();
                pso = pswone.getText().toString();
                pst = pswtwo.getText().toString();

                if (pso.compareTo(pst) != 0) {
                    toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                }else {
                    user.setEmail(em);
                    user.setPassword(pso);
                    register(user);
                    if (state == 2) {
                        toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        myapp.setUser(user);
                        myapp.setLoState(1);

                        intent = new Intent(RegisterActivity.this, Home_Activity.class);
                        startActivity(intent);

                    }else if (state == 1) {
                        toast.makeText(RegisterActivity.this, "账户已存在", Toast.LENGTH_SHORT).show();
                    }else {
                        toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    }
                }
        }
    }

    public void register(User user) {
        Thread t = new Thread(new registerThread());
        t.start();
        try {
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    class registerThread extends Thread {
        @Override
        public void run() {
            state = handleData.handleRegister(user);
            user.printInfo();
        }
    }
}
